Imports System.Data
Imports System.Data.SqlClient
Imports CAPA_Entidad
Imports CAPA_Utilitarios
Public Class UsuarioDatos

    'Se crea una instancia unica para las conexiones
    Dim conex As Conexion = Conexion.getInstance()

    Function obtenerUsuario(ByVal codCIA As String, ByVal codSUC As String, ByVal codUsuario As String, ByVal contrasenia As String)
        Dim usuario As Usuario = Nothing
        Dim con As SqlConnection = conex.obtenerConexion()
        Try
            con.Open()

            Dim query As String = "SELECT CIA, SUC_Codigo, USU_Codigo, USU_Descripcion, USU_Contrasenia, USU_LogPidion, USU_LogPC, " & _
                                  "USU_Picking, USU_Recepcion, USU_Traslados, USU_RegCodBarra, BAN_Codigo, USU_FecBandeja, USU_Activo " & _
                                  "FROM Usuario " & _
                                  "WHERE CIA = @CIA AND SUC_Codigo = @SUC AND USU_Codigo = @USU AND USU_Contrasenia = @CON"

            Dim comando As New SqlCommand(query, con)
            With comando
                .Parameters.AddWithValue("@CIA", codCIA)
                .Parameters.AddWithValue("@SUC", codSUC)
                .Parameters.AddWithValue("@USU", codUsuario)
                .Parameters.AddWithValue("@CON", contrasenia)
            End With
            Dim adapter As New SqlDataAdapter(comando)
            Dim ds As New DataSet
            adapter.Fill(ds)

            If ds.Tables(0).Rows.Count > 0 Then
                usuario = New Usuario(ds.Tables(0).Rows(0)("CIA").ToString.Trim, ds.Tables(0).Rows(0)("SUC_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("USU_Codigo").ToString.Trim, ds.Tables(0).Rows(0)("USU_Descripcion").ToString.Trim, _
                                      ds.Tables(0).Rows(0)("USU_Contrasenia").ToString.Trim, ds.Tables(0).Rows(0)("USU_LogPidion").ToString.Trim, ds.Tables(0).Rows(0)("USU_LogPC").ToString.Trim, ds.Tables(0).Rows(0)("USU_Picking").ToString.Trim, _
                                      ds.Tables(0).Rows(0)("USU_Recepcion").ToString.Trim, ds.Tables(0).Rows(0)("USU_Traslados").ToString.Trim, ds.Tables(0).Rows(0)("USU_RegCodBarra").ToString.Trim, ds.Tables(0).Rows(0)("BAN_Codigo").ToString.Trim, _
                                      ds.Tables(0).Rows(0)("USU_FecBandeja").ToString.Trim, ds.Tables(0).Rows(0)("USU_Activo").ToString.Trim)
            Else
                usuario = New Usuario("-1", "", "", "", "", "", "", "", "", "", "", "", "", "")
            End If
        Catch ex As Exception
            usuario = Nothing
        Finally
            con.Close()
        End Try

        Return usuario
    End Function

End Class
